﻿using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using WPFExcelReport;
namespace IntexReportClient
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    { 
   
        /// Load the workspace values
        /// </summary>
        /// <param name="e"></param>
        protected override void OnStartup(StartupEventArgs e)
        {
            try
            {
                PresentationTraceSources.ResourceDictionarySource.Listeners.Add(new ConsoleTraceListener());
                PresentationTraceSources.ResourceDictionarySource.Switch.Level = SourceLevels.All;
                PresentationTraceSources.DataBindingSource.Listeners.Add(new ConsoleTraceListener());
                PresentationTraceSources.DataBindingSource.Switch.Level = SourceLevels.Error;
                PresentationTraceSources.DependencyPropertySource.Listeners.Add(new ConsoleTraceListener());
                PresentationTraceSources.DependencyPropertySource.Switch.Level = SourceLevels.All;
                PresentationTraceSources.DocumentsSource.Listeners.Add(new ConsoleTraceListener());
                PresentationTraceSources.DocumentsSource.Switch.Level = SourceLevels.All;
                PresentationTraceSources.MarkupSource.Listeners.Add(new ConsoleTraceListener());
                PresentationTraceSources.MarkupSource.Switch.Level = SourceLevels.All;
                PresentationTraceSources.NameScopeSource.Listeners.Add(new ConsoleTraceListener());
                PresentationTraceSources.NameScopeSource.Switch.Level = SourceLevels.All;
                base.OnStartup(e);

                //install unhandled exception handler
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(UnhandledExceptionHandler);
                this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
                TraceConfiguration();
                ThemeManager.Instance.Load();
            }
            catch (Exception ex)
            {
                MessageBox.Show("有错误发生，请删除IntexReportClient.Layout.xml 和workspace.bin后重试");
            }
        } 
       
        /// <summary>
        /// Save the workspace values
        /// </summary>
        /// <param name="e"></param>
        protected override void OnExit(ExitEventArgs e)
        {
            ThemeManager.Instance.Save();
            base.OnExit(e);
        }

        /// <summary>
        /// Manage unhandled exception application wide
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e)
        {
            if (!System.Diagnostics.Debugger.IsAttached)
            {
                Exception ex = e.ExceptionObject as Exception;
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            if (!System.Diagnostics.Debugger.IsAttached)
            {
                Tracer.Error(e.Exception.StackTrace, e.Exception);
            }
        } 
        private void TraceConfiguration()
        {
            try
            {
                //delete the old log file
                string logPath = Path.Combine(PathHelper.ApplicationPath, "IntexReportClient.exe.log");
                DateTime now = System.DateTime.Now;
                if (now.Day == 1)
                {
                    if (File.Exists(logPath))
                        File.Delete(logPath);
                }

                if (IntexReportClient.Properties.Settings.Default.UseTraceListener)
                {
                    //configure the trace
                    System.Diagnostics.Trace.AutoFlush = true;
                    System.Diagnostics.Trace.IndentSize = 2;

                    //configure the text listenner
                    System.Diagnostics.TraceListenerCollection listeners = System.Diagnostics.Trace.Listeners;
                    listeners.Add(new System.Diagnostics.TextWriterTraceListener(logPath, "LOG"));
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
